package com.kuaiditu.assistant.api.xz_api.xz_encode;

import android.text.TextUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Eenie on 2017/8/15.
 * Email: eeniework@gmail.com
 * Des:
 */

public class PwdEncode {

    public static String encode(Date date, String account, String pwd) {
        String key = KeyUtil.buildKey(date, pwd);
        try {
            key = XingZheEncodeUtil.encode(account, key);
        } catch (Exception e) {
            e.printStackTrace();
            key = "";
        }
        return key;
    }


    public static Map<String, String> getLoginMap(String account, String pwd, Date time,String imei) {
        if (!TextUtils.isEmpty(imei) && imei.length() >= 2) {
            imei = imei.substring(imei.length() - 2, imei.length());
        } else {
            imei = "00";
        }

        StringBuffer sb = new StringBuffer();
        sb.append(0);
        sb.append(imei);
        sb.append(dateFormat(time, "ss"));
        sb.append(extractAccount(account));
        sb.append(pwd);
        HashMap var6 = new HashMap();

        var6.put("commonSecret", CommonSecretUtil.getKey(sb.toString()));

        return var6;
    }


    public static String dateFormat(Date time, String unit) {
        SimpleDateFormat var2 = new SimpleDateFormat(unit);
        unit = null;
        String var4;
        try {
            var4 = var2.format(time);
        } catch (Exception var3) {
            var3.printStackTrace();
            var4 = unit;
        }
        return var4;
    }


    public static String extractAccount(String account) {
        String str = replenish(account, 8);
        return str.substring(str.length() - 4, str.length());
    }


    private static String replenish(String src, int len) {
        StringBuffer sb = new StringBuffer();

        if (src.length() < len) {
            for (int i = 0; i < len - src.length(); i++) {
                sb.append("0");
            }
        }
        sb.append(src);
        return sb.toString();
    }


}
